import { reqCategoryAttrList, reqCategoryList1, reqCategoryList2, reqCategoryList3 } from "@/api/product/attr";
import type { Category, CategoryResponseData,Attr,AttrResponseData } from "@/api/product/attr/type";
import { reqSpuList } from "@/api/product/spu";
import type { SpuData, SpuResponseData } from "@/api/product/spu/type";
import { defineStore } from "pinia";
import { ref } from "vue";
const useCategoryStore = defineStore('category', ()=>{
    //一级属性列表
    const category1_attr_list = ref<Category[]>([])
    //获取一级属性列表
    async function getCategory1_attr_list(){
        const result:CategoryResponseData = await reqCategoryList1()
        if(result.code == 200){
            category1_attr_list.value = result.data
        }
        else{
            return Promise.reject(result.message)
        }
    }
    
    //二级属性列表
    const category2_attr_list = ref<Category[]>([])
    //获取二级属性列表
    async function getCategory2_attr_list(category1Id:number){
        const result:CategoryResponseData = await reqCategoryList2(category1Id)
        if(result.code == 200){
            category2_attr_list.value = result.data
        }
        else{
            return Promise.reject(result.message)
        }
    }
    //清空二级属性列表
    function clearCategory2_attr_list(){
        category2_attr_list.value = []
    }
    //三级属性列表
    const category3_attr_list = ref<Category[]>([])
    //获取三级属性列表
    async function getCategory3_attr_list(category2Id:number){
        const result:CategoryResponseData = await reqCategoryList3(category2Id)
        if(result.code == 200){
            category3_attr_list.value = result.data
        }
        else{
            return Promise.reject(result.message)
        }
    }
    //清空三级属性列表
    function clearCategory3_attr_list(){
        category3_attr_list.value = []
    }
    //属性列表
    const attr_list = ref<Attr[]>([])
    //获取属性列表
    async function getAttr_list(category1Id:number,category2Id:number,category3Id:number){
        const result:AttrResponseData = await reqCategoryAttrList(category1Id,category2Id,category3Id);
        if(result.code == 200){
            attr_list.value = result.data
            console.log(attr_list.value);
        }
        else{
            return Promise.reject(result.message)
        }
    }
    //清空属性列表
    function clearAttr_list(){
        attr_list.value = []
    }


    //SPU列表
    const spu_list = ref<SpuData[]>([])
    //SPU总条数
    let SPU_total = ref(0)
    //获取SPU列表
    async function getSpu_list(page:number,limit:number,category3Id:number){
        const result:SpuResponseData = await reqSpuList(page,limit,category3Id);
        if(result.code == 200){
            spu_list.value = result.data.records
            SPU_total.value=result.data.total
        }
        else{
            return Promise.reject(result.message)
        }
    }
    //清空SPU列表
    function clearSpu_list(){
        spu_list.value = []
    }
    //清空所有列表
    function clearAll_list(){
        category1_attr_list.value = []
        clearCategory2_attr_list()
        clearCategory3_attr_list()
        clearAttr_list()
        clearSpu_list()
    }
    return {
        category1_attr_list,
        category2_attr_list,
        category3_attr_list,
        attr_list,
        spu_list,
        SPU_total,
        getCategory1_attr_list,
        getCategory2_attr_list,
        getCategory3_attr_list,
        clearCategory2_attr_list,
        clearCategory3_attr_list,
        getAttr_list,
        clearAttr_list,
        getSpu_list,
        clearSpu_list,
        clearAll_list
    }
})
export default useCategoryStore